﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.BLL;

namespace Maticsoft.Web
{
    public partial class Default : CommonPage
    {
        public DataTable newsDataTable = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Page.Title = commonMod.titile + "-首页";
                //this.Page.Header.Attributes["discription"] = commonMod.description;
                //this.Page.Header.Attributes["keywords"] = commonMod.keywords;
                HtmlMeta keywords = new HtmlMeta(), description = new HtmlMeta(); // keywords, description
                keywords.Name = "keywords";
                keywords.Content = commonMod.keywords;

                description.Name = "description";
                description.Content = commonMod.description;

                this.Page.Header.Controls.Add(keywords);
                this.Page.Header.Controls.Add(description);

                Maticsoft.BLL.IamgeCreateScript.CreateImageScript cis = new Maticsoft.BLL.IamgeCreateScript.CreateImageScript();
                //lbScripts.Text = cis.getCreateImageScript(280, 150);
                ShowNews();
                generateImages();
                ShowLeftNews();
                
            }

        }
        private void ShowNews()
        {
            Maticsoft.BLL.NewsManage.News bll = new Maticsoft.BLL.NewsManage.News();
            this.newsListRepeater.DataSource = bll.GetTopScroll(8);//
            this.newsListRepeater.DataBind();
        }
        private void ShowLeftNews()
        {
            Maticsoft.BLL.NewsManage.News bll = new Maticsoft.BLL.NewsManage.News();
            this.leftNewsRepeater.DataSource = getPagedTable(bll.GetNewsList("").Tables[0], 1, 3);
            this.leftNewsRepeater.DataBind();
        }
        
        private void generateImages()
        {
            //数据
            Maticsoft.BLL.NewsManage.News newbll = new Maticsoft.BLL.NewsManage.News();
            if (null != newbll.getIamgeNewsList())
            {
                newsDataTable = newbll.getIamgeNewsList().Tables[0];

            }
        }

        protected void NewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            DataRowView drv = (DataRowView)e.Item.DataItem;
            HyperLink link = (HyperLink)e.Item.FindControl("newHyperLink");
            if (link != null)
            {
                link.Text = drv["Heading"].ToString();
                link.NavigateUrl = createHref(drv["NewsId"].ToString());


            }
        }
        public string createHref(string newsId)
        {
            StringBuilder strHref = new StringBuilder();
            NewsDto newsDto = null;
            int newId = int.Parse(newsId);
            newsDto = getNavDto(newId);
            string hrefText = PubConstant.getMenuPage()[newsDto.menuIndex];
            if (hrefText != null && hrefText != "")
            {
                strHref.Append(hrefText);
                strHref.Append("?");
                strHref.Append(PubConstant.MENU_PARM);
                strHref.Append("=");
                strHref.Append((int)newsDto.menuIndex);
                strHref.Append("&");
                strHref.Append(PubConstant.NAV_PARM_ONE);
                strHref.Append("=");
                strHref.Append(newsDto.firstNewsClass.ClassId);
                strHref.Append("&");
                strHref.Append(PubConstant.NAV_PARM_TWO);
                strHref.Append("=");
                strHref.Append(newsDto.secondNewsClass.ClassId);
                strHref.Append("&");
                strHref.Append(PubConstant.PARM_NEWSID);
                strHref.Append("=");
                strHref.Append(newsId);


            }
            return strHref.ToString();
        }

        public string createImageNewsHref(string newsId)
        {
            StringBuilder strHref = new StringBuilder();
            NewsDto newsDto = null;
            int newId = int.Parse(newsId);
            newsDto = getNavDto(newId);
            string hrefText = PubConstant.getMenuPage()[newsDto.menuIndex];
            if (hrefText != null && hrefText != "")
            {
                strHref.Append(hrefText);
                strHref.Append("?");
                strHref.Append(PubConstant.MENU_PARM);
                strHref.Append("=");
                strHref.Append((int)newsDto.menuIndex);
                strHref.Append("@");
                strHref.Append(PubConstant.NAV_PARM_ONE);
                strHref.Append("=");
                strHref.Append(newsDto.firstNewsClass.ClassId);
                strHref.Append("@");
                strHref.Append(PubConstant.NAV_PARM_TWO);
                strHref.Append("=");
                strHref.Append(newsDto.secondNewsClass.ClassId);
                strHref.Append("@");
                strHref.Append(PubConstant.PARM_NEWSID);
                strHref.Append("=");
                strHref.Append(newsId);


            }
            return strHref.ToString();
        }

        protected void LeftNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;

            Label createDate = (Label)e.Item.FindControl("createDate");
            HyperLink link = (HyperLink)e.Item.FindControl("leftNewHyperLink");
            if (link != null)
            {
                link.Text = drv["Heading"].ToString();
                link.NavigateUrl = createHref(drv["NewsId"].ToString());

            }
            if (createDate != null)
            {
                createDate.Text = "["+Convert.ToDateTime(drv["IssueDate"]).ToShortDateString()+"]";
            }
        }
    }
}
